package com.syeet.gulimall.cart.vo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 *
 * @author xhl
 * @date 2023/4/25
 */
public class Cart implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<CartItem> items;
    /**
     * 商品总数
     */
    private Integer countNum;
    /**
     * 商品类型数量
     */
    private Integer countType;
    /**
     * 所有商品总价格
     */
    private BigDecimal totalAmount;
    /**
     * 减免价格
     */
    private BigDecimal reduce = new BigDecimal(0);

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public Integer getCountType() {
        int count = 0;
        if (items != null && items.size() > 0) {
            count += items.size();
        }
        return count;
    }


    public BigDecimal getTotalAmount() {
        //1.计算购物项总价
        BigDecimal amount = new BigDecimal(0);
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                if (item.getCheck()) {
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }

            }
        }
        //2.减去优惠价格
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }


    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
